package com.keba.lamp.common.utils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.IOException;

public class FooterEvent extends PdfPageEventHelper {
//    //总页数
//    PdfTemplate totalPage;
    int startPage = 1;
    //字体
    Font font;
    private Paragraph phrase;
    public FooterEvent(Integer startPage) {
        this.startPage = startPage;
    }

    {
        try {
            BaseFont bf = BaseFont.createFont("SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            font = new Font(bf, 12, Font.NORMAL, BaseColor.BLACK);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }
    public void onEndPage(PdfWriter writer, Document document) {
        float pageWidth = document.getPageSize().getWidth();
        //1cm = 28.35磅 页脚下边距1.27cm，36磅
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
                new Phrase("第 " + (document.getPageNumber() + startPage - 1) + " 页", font), pageWidth / 2, 36F, 0);
    }

}
